package com.example.heying.worldcupapp2018;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.heying.worldcupapp2018.R;
import com.google.gson.Gson;
import com.example.heying.adapter.MyAdapter;
import com.example.heying.bean.Football;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class TwoActivity extends AppCompatActivity {
    private List<Football.ItemDTO> footballList;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        initView();
        initData();
    }
    protected  void initView(){
        listView = findViewById(R.id.two_listview);
    }
    protected  void initData(){

        String json = getJson(this, "football.json");
        Gson gson = new Gson();
        footballList = gson.fromJson(json, Football.class).getItem();

        listView.setAdapter(new MyAdapter(TwoActivity.this,footballList));
    }

    public String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取本地数据资源管理器
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}